# -*- coding: utf-8 -*-
from datetime import datetime

import sqlalchemy as sa
from sqlalchemy import DateTime
from sqlalchemy.dialects import mysql

from sqlalchemy.orm import Mapped, mapped_column, relationship

from App.app.admin.models.m2m import sys_role_menu
from App.common.model import Base, id_key
from App.utils.timezone import timezone


class SysRole(Base):
    """系统角色表"""

    __tablename__ = 'sys_role'

    id: Mapped[id_key] = mapped_column(init=False)
    name: Mapped[str] = mapped_column(sa.String(20), default='', sort_order=2, comment='角色名称')
    status: Mapped[int] = mapped_column(sa.INTEGER(), default=1, sort_order=3, comment='角色状态（0停用 1正常）')
    remark: Mapped[str | None] = mapped_column(mysql.LONGTEXT(), default=None, sort_order=4, comment='备注')
    created_time: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), init=False, default_factory=timezone.now, sort_order=999, comment='创建时间'
    )
    updated_time: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), init=False, onupdate=timezone.now,sort_order=999, comment='更新时间'
    )
    # 角色用户一对多
    users: Mapped[list["SysUser"]] = relationship(init=False, back_populates="role")  # noqa: F821

    # 角色菜单多对多
    menus: Mapped[list['SysMenu']] = relationship(init=False, secondary=sys_role_menu, back_populates='roles')  # noqa: F821